package Kernel;

import java.io.*;
import java.util.*;

public class Place {
	private AbstractSet<Arc_T2P> PreArcSet = new HashSet<Arc_T2P>();
	private AbstractSet<Arc_P2T> PostArcSet = new HashSet<Arc_P2T>();
	private long marking=0;
	private long capacity;
	
	static List<Place> PlaceSet = new ArrayList<Place>();
	
	public int PlaceIndex;
	
	public Place() {
		this.marking = 0;
		this.capacity = Long.MAX_VALUE;
		AddToSet();
	}
	public Place(long marking, long capacity) {
		this.marking = marking;
		this.capacity = capacity;
		AddToSet();
	}
	public Place(long marking) {
		this.marking = marking;
		this.capacity = Long.MAX_VALUE;
		AddToSet();
	}
	public Place(ObjectInputStream ois) throws IOException {
		this.marking = ois.readLong();
		this.capacity = ois.readLong();
		AddToSet();
	}
	
	private void AddToSet() {
		PlaceSet.add(this);
		PlaceIndex = PlaceSet.indexOf(this)+1;		
	}
	
	public long GetMarking() {
		return this.marking;
	}
	public long GetCapacity() {
		return this.capacity;
	}
	
	public void FireToken(long nToken){
		if ( nToken > this.marking )
			throw new RuntimeException();
		this.marking -= nToken;
	}
	public void FillToken(long nToken){
		if ( nToken + this.marking > this.capacity )
			throw new RuntimeException();
		this.marking += nToken;
	}
	public void SetToken(long nToken){
		if ( nToken > this.capacity )
			throw new RuntimeException();
		this.marking = nToken;
	}
	
	public void AddPreArc(Arc_T2P preArc){
		PreArcSet.add(preArc);
	}
	public void AddPostArc(Arc_P2T postArc){
		PostArcSet.add(postArc);
	}
	public void RemovePreArc(Arc_T2P preArc){
		PreArcSet.remove(preArc);
	}
	public void RemovePostArc(Arc_P2T postArc){
		PostArcSet.remove(postArc);
	}
	AbstractSet<Arc_T2P> GetPreArcSet(){
		return PreArcSet;
	}
	AbstractSet<Arc_P2T> GetPostArcSet(){
		return PostArcSet;
	}
	
	public void Store(ObjectOutputStream oos) throws IOException{
		oos.writeLong(marking);
		oos.writeLong(capacity);
	}
}
